package com.bcu.cn.onlineexamsystem.questionmanage.mapper;

import com.bcu.cn.onlineexamsystem.questionmanage.entity.QuestionManage;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionManageMapper {
    
    @Select("SELECT * FROM questions WHERE teacher_id = #{teacherId}")
    List<QuestionManage> findByTeacherId(String teacherId);
    
    @Select("SELECT * FROM questions WHERE question_id = #{questionId}")
    QuestionManage findById(Long questionId);
    
    @Insert("INSERT INTO questions(course_id, type, content, options, answer, score, teacher_id) " +
            "VALUES(#{courseId}, #{type}, #{content}, " +
            "CASE WHEN #{options} IS NULL THEN NULL ELSE CAST(#{options} AS JSON) END, " +
            "#{answer}, #{score}, #{teacherId})")
    @Options(useGeneratedKeys = true, keyProperty = "questionId")
    int insert(QuestionManage question);
    
    @Update("UPDATE questions SET course_id = #{courseId}, type = #{type}, content = #{content}, " +
            "options = CASE WHEN #{options} IS NULL THEN NULL ELSE CAST(#{options} AS JSON) END, " +
            "answer = #{answer}, score = #{score} " +
            "WHERE question_id = #{questionId} AND teacher_id = #{teacherId}")
    int update(QuestionManage question);
    
    @Delete("DELETE FROM questions WHERE question_id = #{questionId} AND teacher_id = #{teacherId}")
    int delete(Long questionId, String teacherId);
    
    @Select("SELECT * FROM questions WHERE course_id = #{courseId} AND teacher_id = #{teacherId}")
    List<QuestionManage> findByCourseId(String courseId, String teacherId);
} 